package com.nkzy.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Create by 王建烨 on 2022/3/21 9:03
 * 用SpringMVCConfig类代替SpringMVC的核心配置文件
 **/

//@Configuration // 声明配置类
//@EnableWebMvc // 启用与Web相关的注解
//@ComponentScan({"com.nkzy.controller","com.nkzy.common"})
//public class SpringMVCConfig implements WebMvcConfigurer {
//    @Value("${front.url}")
//    private String frontUrl;
//
//    /**
//     * 文件上传
//     */
//    @Bean
//    public MultipartResolver multipartResolver(){
//        return new StandardServletMultipartResolver();
//    }
//
//    /**
//     * 跨域访问
//     */
//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        registry.addMapping("users/**")
//                .allowedOrigins(frontUrl)
//                .allowCredentials(true);
//    }
//}

//SpringMVC的配置类代替其配置文件
@Configuration
@EnableWebMvc  //打开与Web开发相关的注解，如：@ResponseBody\@RequestBody等
@ComponentScan({"com.nkzy.controller","com.nkzy.common"})
public class SpringMVCConfig implements WebMvcConfigurer {
    @Value("${front.url}")
    private String frontUrl;

    //文件上传的处理器
    @Bean
    public MultipartResolver multipartResolver() {
        return new StandardServletMultipartResolver();
    }

    //跨域访问:浏览器端访问，IP地址或端口不一样
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/users/**").allowedOrigins(frontUrl).allowCredentials(true);
    }
}